#!/bin/sh
#
# Tomcat下载安装方法
#
# @author liuzy
# @since 2016-07-02

F_debug "F_F" "加载tomcat.sh"

### Tomcat下载根路径
tomcatBasePath="http://mirrors.aliyun.com/apache/tomcat"

### 默认Tomcat版本号信息
t6Version="6.0.45"
t7Version="7.0.70"
t8Version="8.0.36"
t9Version="9.0.0.M8"

### 获取Tomcat版本号
## 参数
# $1 版本号 默认Tomcat7
## 返回
# Tomcat版本号
function F_getTomcatVersion() {
	F_debug "F_getTomcatVersion" "获取Tomcat版本号"
	if [[ $1 ]] ; then
		if [[ "$1" = "6" ]] ; then
			F_return ${t6Version}
		elif [[ "$1" = "7" ]] ; then
			F_return ${t7Version}
		elif [[ "$1" = "8" ]] ; then
			F_return ${t8Version}
		elif [[ "$1" = "9" ]] ; then
			F_return ${t9Version}
		fi
	else
		F_return ${t7Version}
	fi
}

### 获取Tomcat下载地址
## 参数
# $1 版本号 默认Tomcat7
## 返回
# 下载地址
function F_getTomcatDlUrl() {
	F_debug "F_getTomcatDlUrl" "获取Tomcat下载地址"
	# 获取Tomcat版本号
	F_getTomcatVersion $1
	# 返回
	F_return "${tomcatBasePath}/tomcat-${F_result:0:1}/v${F_result}/bin/apache-tomcat-${F_result}.tar.gz"
}

### 下载指定版本Tomcat到当前目录
## 参数
# $1 版本号
## 返回
# 下载后的文件名
function F_dlTomcat() {
	F_debug "F_dlTomcat" "下载指定版本Tomcat到当前目录"
	# 获取Tomcat下载地址
	F_getTomcatDlUrl $1
	# 下载
	wget -c ${F_result}
	# 返回文件名
	F_return ${F_result##*/}
}

### 下载指定版本Tomcat到指定目录
## 参数
# $1 版本号
# $2 保存路径 默认当前目录
## 返回
# 下载后的文件名
function F_dlTomcat2Path() {
	F_debug "F_dlTomcat2Path" "下载指定版本Tomcat到指定目录"
	# 获取Tomcat下载地址
	F_getTomcatDlUrl $1
	# 输入保存路径
	savePath=$2
	if [[ ${savePath} ]] ; then
		# 创建目录
		if [[ ! -d ${savePath} ]] ; then
			mkdir -p ${savePath}
		fi
		# 下载到指定目录
		wget -c ${F_result} －O ${savePath}/${F_result##*/}
	else
		# 下载到当前目录
		wget -c ${F_result}
	fi
	# 返回文件名
	F_return ${F_result##*/}
}

### 清理Tomcat目录(bin/*.bat和webaaps/*)
## 参数
# $1 目录
## 返回
# 无
function F_clearTomcat() {
	F_debug "F_clearTomcat" "清理Tomcat目录"
	F_assert "F_clearTomcat" "目录" $1
	if [[ -d $1 ]] ; then
		# 删除bat文件
		rm -rf $1/bin/*.bat
		# 删除webapps下文件
		rm -rf $1/webapps/*
		# 清理catalina.sh
		sed -i -e 3,105d $1/bin/catalina.sh
		sed -i '2 a\#JAVA_HOME=""' $1/bin/catalina.sh
		sed -i '3 a\#CATALINA_HOME=""' $1/bin/catalina.sh
		sed -i '4 a\#TOMCAT_HOME=""' $1/bin/catalina.sh
		sed -i '5 a\#JAVA_OPTS="-server"' $1/bin/catalina.sh
		sed -i '6 a\#JAVA_OPTS="$JAVA_OPTS -Xms128M"' $1/bin/catalina.sh
		sed -i '7 a\#JAVA_OPTS="$JAVA_OPTS -Xmx512M"' $1/bin/catalina.sh
		# 清理server.xml
		F_configTomcat $1 "8005" "8080"
	else
		F_debug "F_clearTomcat" "输入的Tomcat目录$1不存在！"
	fi
}

### 安装并解压Tomcat
## 参数
# $1 版本号
## 返回
# 无
function F_installTomcat() {
	F_debug "F_installTomcat" "安装并解压Tomcat"
	# 下载到当前目录得到文件名
	F_dlTomcat $1
	# 解压
	tar xf ${F_result}
	# 清理Tomcat
	F_clearTomcat ${F_result/.tar.gz/}
}

### 配置Tomcat(修改server.xml文件)
## 参数
# $1 Tomcat的目录
# $2 停止端口号
# $3 WEB端口号
## 返回
# 无
function F_configTomcat() {
	F_debug "F_configTomcat" "配置Tomcat"
	F_assert "F_configTomcat" "Tomcat的目录" $1
	F_assert "F_configTomcat" "停止端口号" $2
	F_assert "F_configTomcat" "WEB端口号" $3
	echo "<?xml version='1.0' encoding='utf-8'?>" > $1/conf/server.xml
	echo '<Server port="${$2}" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="off" />
  <Listener className="org.apache.catalina.core.JasperListener" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector port="${$3}"
        protocol="org.apache.coyote.http11.Http11AprProtocol"
        URIEncoding="UTF-8"
        minSpareThreads="25"
        enableLookups="false"
        disableUploadTimeout="true"
        connectionTimeout="20000"
        acceptCount="300"
        maxThreads="300"
        compression="on"
        compressionMinSize="2048"
        compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,application/json"
        redirectPort="8443"/>
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>
      <Host name="localhost" appBase="webapps" unpackWARs="false" autoDeploy="false">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
  </Service>
</Server>' >> $1/conf/server.xml
}
